package com.podium.comment.validation.validator;

import com.podium.comment.utils.StringUtils;
import com.podium.comment.validation.annotation.Password;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.regex.Pattern;

/**
 * @author 邵文泽
 * @description 密码验证器
 * @date 2025/5/14 10:17
 */
public class PasswordValidator implements ConstraintValidator<Password,String> {

    private static final Pattern PASSWORD = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*]).{8,20}$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (!StringUtils.hasLength(value)){
            return true;
        }
        return PASSWORD.matcher(value).matches();
    }
}
